package com.lj.demo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.demo.eduservice.client.VodClient;
import com.lj.demo.eduservice.entity.EduVideo;
import com.lj.demo.eduservice.entity.VideoInfoForm;
import com.lj.demo.eduservice.mapper.EduVideoMapper;
import com.lj.demo.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.demo.servicebase.exception.AException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2020-12-03
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    //添加小节
    @Override
    public void addVideInfoForm(VideoInfoForm videoInfoForm) {
        EduVideo eduVideo = new EduVideo();
        if (videoInfoForm != null) {
            BeanUtils.copyProperties(videoInfoForm, eduVideo);
            int insert = baseMapper.insert(eduVideo);
            if (insert == 0){
                throw new AException(20001,"添加失败,请联系管理员");
            }
        }
    }

    //修改小节
    @Override
    public void editVideInfoForm(VideoInfoForm videoInfoForm) {
        EduVideo eduVideo = new EduVideo();
        if (videoInfoForm != null) {
            BeanUtils.copyProperties(videoInfoForm, eduVideo);
            int insert = baseMapper.updateById(eduVideo);
            if (insert == 0){
                throw new AException(20001,"修改失败,请联系管理员");
            }
        }
    }

    //很具id查询小节
    @Override
    public VideoInfoForm findVideoById(String id) {
        VideoInfoForm videoInfoForm =  new VideoInfoForm();
        EduVideo eduVideo = baseMapper.selectById(id);
        if (eduVideo == null){
            throw new AException(20001,"数据不存在");
        }BeanUtils.copyProperties(eduVideo,videoInfoForm);
        return videoInfoForm;
    }

    @Override
    public boolean removeViodeByid(String id) {
        //根据小节id获取视频id
        EduVideo eduVideo = this.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里是否有id
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id，远程调用实现视频删除
            vodClient.removeVideo(videoSourceId);
        }
        boolean b = this.removeById(id);
        return b;
    }

    //批量删除视频
    @Override
    public boolean removeByCourseId(String courseId){
        //根据课程id查询视频id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(queryWrapper);

        //得到所有视频列表的云端原始视频
        List<String> videoCourseIdList = new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            EduVideo eduVideo = videoList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                videoCourseIdList.add(videoSourceId);
            }
        }

        //调用vod服务删除远程视频
        if (videoCourseIdList.size() > 0){
            vodClient.removeVideoList(videoCourseIdList);
        }

        //删除video表示的记录
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        Integer b = baseMapper.delete(queryWrapper1);
        return null != b && b >0;
    }
}
